<?php
namespace GK;
class Controller
{
    public function __construct(){
        // $this->InitService();
        // $this->beforeAction();
        // Hook::listen('action_begin',$this->config);
        //实例化视图类
        // $this->view=Think::instance('Think\View');
        //控制器初始化
        if(method_exists($this,'__InitService')){
            $this->__InitService();
        }
        if(method_exists($this,'__BeforeActionList')){
            $this->__BeforeActionList();
        }
    }
    //初始化操作
    protected function _InitService(){
    }
    //前置操作
    protected function _BeforeActionList(){
    }
    public function assign($tpl_var,$tpl_value=null)
    {  //发送变量
        if(is_array($tpl_var) && count($tpl_var) > 0)
        {
            foreach ($tpl_var as $k => $v) 
            {
                $this->tpl_vars[$k] = $v;
            }
        }elseif($tpl_var)
        {
            $this->tpl_vars[$tpl_var] = $tpl_value;
        }
    }
    public function display($files){
        //渲染模板
        $viewpath=APP_PATH.App::$Module.DS.'View';
        $file=$viewpath.DS.$files.'.html';
        if(is_file($file)){
            $loader = new \Twig_Loader_Filesystem($viewpath);
            if(App::$Config['AppConfig']['app_debug']){
                $twig = new \Twig_Environment($loader, array(
                    'cache'=>false,
                    'debug' => true,
                ));
            }else{
                $twig = new \Twig_Environment($loader, array(
                    'cache' =>RUNTIME_PATH.'Cache',
                    'auto_reload' => true,
                ));
            }
            $template = $twig->loadTemplate($files.'.html');
            $template->display($this->tpl_vars);
        }
    }
    public function __call($name,$argv)
    {
        Log::Info($_SERVER['REMOTE_ADDR'].':Request error\'s URL-> '.$_SERVER['HTTP_HOST'].'/'.CONTROL_NAME.'/'.ACTION_NAME);
        ErrorMsg::output(404,"Errormsg");   
    }
}